//--------------------------------------------------------------------------------
// If Using prototype.js, please commentout this code.
// var $ = function(args) { return document.getElementById(args); };

//--------------------------------------------------------------------------------
// Focus Class
//--------------------------------------------------------------------------------
var Focus = {
    Index:  {},
    BackColor:  "RGB(255,255,204)",
    LostColor:  "RGB(255,255,255)",

    GotFocus:   function(self) {
        self.style.backgroundColor = Focus.BackColor;
        self.select();
    },
    LostFocus:  function(self) { self.style.backgroundColor = Focus.LostColor; },
    NextIndex:  function(key) {
        try { document.getElementById(Focus.Index[key]).focus(); }
        catch( e ) {}
    }
}
//--------------------------------------------------------------------------------
// Validator
//--------------------------------------------------------------------------------
var Validator = {
    Validation: function(self, keyCode, validator) {
        var t = document.getElementById(self.id).type;
        if ((t == "text" && keyCode == 13) ||
	        (t == "password" && keyCode == 13) ||
    	    (t == "radio" && keyCode == 13) ||
            (t == "textarea" && keyCode == 9)) {
            try{
                if (event.preventDefault) { event.preventDefault(); }
                else { event.returnValue = false; }
            } catch( e ) { // firefoxの場合エラーになるので
            } finally {
                self.blur();
                if (validator) {
                    var err = "";
                    for (var v in validator) {
                        switch (v) {
                            case "NotEmpty":
                            case "IsNumeric":
                            case "IsEmail":
                            case "IsURL":                    
                            case "IsTime":
                                //改行を除く
                                var str = self.value.replace(/\n|\r\n/g,"");
                                err = eval("Validator." + v + "('" + str + "')");
                                break;
                            case "Range":
                                rangearray = validator[v]
                                err = eval("Validator." + v + "('" + self.value + "'," + rangearray[0] + "," + rangearray[1] + ")");
                                break;
                            case "SameValue":
                                err = eval("Validator." + v + "('" + self.value + "','" + validator[v] + "')");
                                break;
                            default:
                                err = "指定したValidatorがありません。";
                                break;
                        }
                        if (err != "") { break; }
                    }
                    if (err != "") {
                        Validator.ShowAlert(self, err);
                    }
                    else { Focus.NextIndex(self.id);}
                }
                else { Focus.NextIndex(self.id); }
            }
        }
    },
    NotEmpty:   function (val) {
        var ret = "";
        checkvalue = val;
        checkvalue = checkvalue.replace(/^[ ]+|[ ]+$/g, '');
        if (checkvalue.length == 0) { ret = "必須入力です。"; }
        return ret;
    },
    IsNumeric:  function (val) {
        var ret = "";
        checkvalue = val;
        if (checkvalue.length > 0) {
	        if (checkvalue.match(/[^0-9]+(\.[0-9]+)?$/g)) { ret = "数値を入力してください。"; }
        }
        return ret;
    },
    Range:      function (val, min, max) {
        var ret = "";
        var err = "";
        checkvalue = val;
        if (checkvalue.length > 0) {    
            err = Validator.IsNumeric(checkvalue);
            if (err == "") { 
                if (min > checkvalue || max < checkvalue) { ret = min + "～" + max + "の範囲で入力してください。"; }
            }
            else { ret = err; }
        }
        return ret;
    },
    IsEmail:    function (val) {
        var ret = "";
        checkvalue = val;   
        if (checkvalue.length > 0) {
            if (checkvalue.match(/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/)) { ret = ""; }
            else { ret = "不正なメールアドレスです。"; }
        }
        return ret;
    },
    IsURL:      function (val) {
        var ret = "";
        checkvalue = val;
        if (checkvalue.length > 0) {
            if (checkvalue.match(/^(http|https|ftp):\/\/\S+\.\S+$/)) { ret = ""; }
            else { ret = "不正なＵＲＬです。"; }
        }
        return ret;
    },
    IsTime:     function (val) {
        var ret = "";   
        checkvalue = val;
        if (checkvalue.length > 0) {
            if (!checkvalue.match(/^[0-9]{1,2}:[0-9]{2}$/)) { ret = "時刻(hh:mm)を入力してください。"; }
            var hhmm = checkvalue.split(":");
            if (hhmm[0] > 24 || hhmm[1] > 59) { ret = "時刻(hh:mm)が不正です。"; }
        }
        return ret;
    },
    SameValue:  function (val, id) {
        var ret = "";
        checkvalue = val;
        idvalue = document.getElementById(id).value;
        if (idvalue.length > 0) {
            if (checkvalue != idvalue) { ret = "入力値が異なっています。"; }
        }
        return ret;
    },
    ShowAlert:  function (self, err) {
        try {
            Dialog.alert(err, {className: "alphacube", width:300, okLabel: "閉じる",
                                 onOk:function() { Focus.GotFocus(self); return true; }}); }
        catch( e ) { alert(err); Focus.GotFocus(self);}
    }
}

//--------------------------------------------------------------------------------
// HTTP通信
//--------------------------------------------------------------------------------
function CreateXMLHttpRequest(cbFunc) {
    var XMLhttpObject = null;
    try{
        XMLhttpObject = new XMLHttpRequest();
    } catch(e) {
        try{
            XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try{
                XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){ return null; }
        }
    }
    if (XMLhttpObject) XMLhttpObject.onreadystatechange = cbFunc;
    
    return XMLhttpObject;
}

